import pandas as pd
import time, csv


def cst_to_str(cstTime):
    tempTime = time.strptime(cstTime,'%Y年%m月%d日')
    resTime = time.strftime('%Y-%m-%d',tempTime)
    return resTime

file = "日经225指数历史数据.csv"
df = pd.read_csv(file)

df = df.rename(columns={'日期':'open_time', "收盘": "close", "开盘": "open",
                        "高": "high", "低":"low", "交易量": "volume"})
df = df.drop(labels="涨跌幅", axis=1)  # axis=1 表示按列删除，删除gender列

df['open_time'] = df['open_time'].map(lambda x: cst_to_str(x))
df['close'] = df['close'].map(lambda x: x.replace(",", ''))
df['open'] = df['open'].map(lambda x: x.replace(",", ''))
df['high'] = df['high'].map(lambda x: x.replace(",", ''))
df['low'] = df['low'].map(lambda x: x.replace(",", ''))
df['volume'] = df['volume'].map(lambda x: x.replace("M", '').replace("B", '').replace("K", ''))

df.set_index('open_time', drop=True, inplace=True)
df.sort_values("open_time", inplace=True)
df.to_csv("data.csv", quoting=csv.QUOTE_NONE)
